package cn.taoticket.service.impl;

import cn.taoticket.client.*;
import cn.taoticket.common.*;
import cn.taoticket.exception.ItemErrorCode;
import cn.taoticket.pojo.*;
import cn.taoticket.service.ItemDetailService;
import cn.taoticket.vo.ItemDetailVo;
import cn.taoticket.vo.ItemPriceVo;
import cn.taoticket.vo.ItemSchedulerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ItemDetailServiceImpl implements ItemDetailService {
    @Resource
    private RestTaoticketItemClient restTaoticketItemClient;
    @Resource
    private RestTaoticketImageClient restTaoticketImageClient;
    @Resource
    private RestTaoticketCinemaClient restTaoticketCinemaClient;
    @Resource
    private RestTaoticketSchedulerClient restTaoticketSchedulerClient;
    @Resource
    private RestTaoticketSchedulerSeatPriceClient restTaoticketSchedulerSeatPriceClient;
    @Resource
    private RestTaoticketSchedulerSeatClient restTaoticketSchedulerSeatClient;

    @Override
    public Dto<ItemDetailVo> queryItemDetail(Long id) throws Exception {
        TaoticketItem taoticketItem = restTaoticketItemClient.getTaoticketItemById(id);
        if (EmptyUtils.isEmpty(taoticketItem)) {
            return null;
        }
        //查询图片
        List<TaoticketImage> imageList = getImageList(taoticketItem.getId(), Constants.Image.ImageType.carousel, Constants.Image.ImageCategory.item);
        //查询剧院
        TaoticketCinema taoticketCinema = restTaoticketCinemaClient.getTaoticketCinemaById(taoticketItem.getCinemaId());
        //组装返回数据
        ItemDetailVo itemDetailVo = copyData(taoticketItem, taoticketCinema, imageList);
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }

    @Override
    public Dto<List<ItemSchedulerVo>> queryItemScheduler(Long id) throws Exception {
        //根据商品主键查询商品信息
        TaoticketItem taoticketItem = restTaoticketItemClient.getTaoticketItemById(id);
        if (EmptyUtils.isEmpty(taoticketItem)) {
            //没有则抛出没有商品异常
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        //查询对应的排期列表
        Map<String, Object> map = new HashMap<>();
        map.put("itemId", id);
        List<TaoticketScheduler> schedulerList = restTaoticketSchedulerClient.getTaoticketSchedulerListByMap(map);
        if (EmptyUtils.isEmpty(schedulerList)) {
            //没有则抛出没有商品异常
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        ItemSchedulerVo itemSchedulerVo;
        //组装返回数据
        List<ItemSchedulerVo> itemSchedulerVoList = new ArrayList<>();
        for (int i = 0; i < schedulerList.size(); i++) {
            itemSchedulerVo = new ItemSchedulerVo();
            BeanUtils.copyProperties(taoticketItem, itemSchedulerVo);
            BeanUtils.copyProperties(schedulerList.get(i), itemSchedulerVo);
            itemSchedulerVo.setStartTime(DateUtil.format(schedulerList.get(i).getStartTime()));
            itemSchedulerVo.setEndTime(DateUtil.format(schedulerList.get(i).getEndTime()));
            itemSchedulerVoList.add(itemSchedulerVo);
        }
        return DtoUtil.returnDataSuccess(itemSchedulerVoList);
    }

    @Override
    public Dto<List<ItemPriceVo>> queryItemPrice(Long scheduleId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("scheduleId", scheduleId);
        //根据排期id查询排期价格信息
        List<TaoticketSchedulerSeatPrice> schedulerSeatPriceList = restTaoticketSchedulerSeatPriceClient.getTaoticketSchedulerSeatPriceListByMap(map);
        if (EmptyUtils.isEmpty(schedulerSeatPriceList)) {
            throw new BaseException(ItemErrorCode.ITEM_NO_DATA);
        }
        ItemPriceVo itemPriceVo;
        Map<String, Object> lockMap;
        //组装返回显示信息
        List<ItemPriceVo> itemPriceVoList = new ArrayList<>();
        for (TaoticketSchedulerSeatPrice taoticketSchedulerSeatPrice : schedulerSeatPriceList) {
            itemPriceVo = new ItemPriceVo();
            BeanUtils.copyProperties(taoticketSchedulerSeatPrice, itemPriceVo);
            //查询所有有效并且是没有被锁定的座位
            lockMap = new HashMap<>();
            //1，代表有座位,0代表无座位
            lockMap.put("status", 1);
            lockMap.put("scheduleId", taoticketSchedulerSeatPrice.getScheduleId());
            int num = restTaoticketSchedulerSeatClient.getTaoticketSchedulerSeatCountByMap(lockMap);
            int isHasSeat = num > 0 ? 1 : 0;
            itemPriceVo.setIsHaveSeat(isHasSeat);
            itemPriceVoList.add(itemPriceVo);
        }
        return DtoUtil.returnDataSuccess(itemPriceVoList);
    }

    /**
     * 组装数据
     */
    private ItemDetailVo copyData(TaoticketItem taoticketItem, TaoticketCinema taoticketCinema, List<TaoticketImage> imageList) throws ParseException {
        ItemDetailVo itemDetailVo = new ItemDetailVo();
        BeanUtils.copyProperties(taoticketItem, itemDetailVo);
        itemDetailVo.setStartTime(DateUtil.format(taoticketItem.getStartTime()));
        itemDetailVo.setEndTime(DateUtil.format(taoticketItem.getEndTime()));
        itemDetailVo.setState(taoticketItem.getState() + "");
        itemDetailVo.setImgUrl(imageList.get(0).getImgUrl());
        if (EmptyUtils.isEmpty(taoticketCinema)) {
            //封装剧院信息
            BeanUtils.copyProperties(taoticketCinema, itemDetailVo);
        }
        itemDetailVo.setId(taoticketItem.getId());
        return itemDetailVo;
    }


    /**
     * 查询图片
     */
    private List<TaoticketImage> getImageList(Long id, Integer type, Integer category) throws Exception {
        Map<String, Object> paramImage = new HashMap<>();
        paramImage.put("targetId", id);
        paramImage.put("type", type);
        paramImage.put("category", category);
        return restTaoticketImageClient.queryImageList(id, type, category);
    }
}
